package cn.gson.fengboot.model.service;

import cn.gson.fengboot.model.dao.*;
import cn.gson.fengboot.model.mapper.ZchukuMapper;
import cn.gson.fengboot.model.mapper.ZrukuMapper;
import cn.gson.fengboot.model.pojos.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ZrukuService {
    @Autowired
    ZrukuMapper mapper;
    @Autowired
    ZrukuDao dao;
    @Autowired
    ZrukuxqDao xqdao;
    @Autowired
    ZreturnedgoodDao thdao;
    @Autowired
    ZbarterDao hhdao;

    public List<Zruku> chukuliebiao(String search, String search2){
        return mapper.chukuliebiao(search,search2);
    };

    public void add(Zruku rk){
        Zruku save = dao.save(rk);
        if(save.getReturgoodId().getReturgoodId()!=-1){
            Zreturnedgood th = thdao.findById(save.getReturgoodId().getReturgoodId()).get();
            th.setReturgoodState("已入库");
            thdao.save(th);
        }else {
            Zbarter hh = hhdao.findById(save.getBarterId().getBarterId()).get();
            hh.setBarterState("已入库");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time = df.format(new Date());
            Timestamp ts = Timestamp.valueOf(time);
            hh.setBarterThtime(ts);
            hhdao.save(hh);
        }
        save.getXq().forEach(a->{
            a.setRukuId(save);
        });
        xqdao.saveAll(save.getXq());
    }

    //<!--谭方圆=>客户合并的入库查询-->
    public List<Zruku> tfyruku(Integer id1, Integer id2){
        return mapper.tfyruku(id1,id2);
    }

    public List<Zrukuxq> rukuxq(Integer search){
        return mapper.rukuxq(search);
    }
}
